﻿using System;
using System.Windows;
using System.Windows.Controls;
using VMWorkshop.Domain;
using VMWorkshop.Silverlight.Models.ViewModels;

namespace VMWorkshop.Silverlight.Views
{
    public partial class Demo : Page
    {
        private DemoViewModel _vm;

        public Demo()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Demo_Loaded);
        }

        void Demo_Loaded(object sender, RoutedEventArgs e)
        {
            this._vm = new DemoViewModel(
                    new Uri("/products/list", UriKind.Relative),
                    new Uri("/products/edit", UriKind.Relative),
                    new Uri("/products/persist", UriKind.Relative)
                    );

            this.DataContext = this._vm;

            this._vm.GetProducts();
        }

        private void productsGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this._vm.GetSelectedProduct(((ProductListView)e.AddedItems[0]).ProductId);
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            this._vm.Persist();
        }
    }
}